import { watch } from "vue";
import { polling } from "./../utils/index";
import { createRouter, createWebHashHistory, RouteRecordRaw } from "vue-router";
import Cookies from "js-cookie";
import store from "@/store";
import { initMenus } from "@/utils/menus";

const routes: Array<RouteRecordRaw> = [
    {
        path: "/",
        name: "root",
        redirect: "/admin",
    },
    // {
    //     path: "/admin",
    //     name: "admin",
    //     component: () => import("@/layouts/admin.vue"),
    //     redirect: "/admin/dashboard",
    //     children: [
    //         {
    //             path: "/admin/dashboard",
    //             name: "dashboard",
    //             component: () => import("@/views/dashboard/dashboard.vue"),
    //         },
    //     {
    //         path: "/admin/user",
    //         name: "user",
    //         component: () => import("@/views/user/user.vue"),
    //     },
    //     {
    //         path: "/admin/menu",
    //         name: "menu",
    //         component: () => import("@/views/menu/menu.vue"),
    //     },
    //     {
    //         path: "/admin/cat",
    //         name: "cat",
    //         component: () => import("@/views/cat/cat.vue"),
    //     },
    //     {
    //         path: "/admin/banner",
    //         name: "banner",
    //         component: () => import("@/views/banner/banner.vue"),
    //     },
    //     {
    //         path: "/admin/role",
    //         name: "role",
    //         component: () => import("@/views/role/role.vue"),
    //     },
    //     {
    //         path: "/admin/access",
    //         name: "access",
    //         component: () => import("@/views/access/access.vue"),
    //     },
    //     {
    //         path: "/admin/goodsType",
    //         name: "goodsType",
    //         component: () => import("@/views/goodsType/goodsType.vue"),
    //     },
    //     {
    //         path: "/admin/goodsTypeAttribute",
    //         name: "goodsTypeAttribute",
    //         component: () => import("@/views/goodsTypeAttribute/goodsTypeAttribute.vue"),
    //     },
    //     ],
    // },
    {
        path: "/login",
        name: "login",
        component: () => import("@/layouts/login.vue"),
    },
    {
        path: "/404",
        name: "notFount",
        component: () => import("@/layouts/404.vue"),
        // async beforeEnter(to, from, next) {
        //     const redirectedFromPath = to.redirectedFrom?.path;
        //     const redirectedFromQuery = to.redirectedFrom?.query;
        //     await polling(() => store.state.menuDone);
        //     const currentRoutes = router.getRoutes();
        //     if (currentRoutes.some(item => item.path === redirectedFromPath) && redirectedFromPath) {
        //         next({ path: redirectedFromPath, query: redirectedFromQuery });
        //     } else {
        //         next();
        //     }
        // },
    },
    // {
    //     path: "/:pathMathch(.*)",
    //     name: "404",
    //     redirect: "/404",
    // },
];

const router = createRouter({
    history: createWebHashHistory(),
    routes,
});

router.beforeEach(async (to, from, next) => {
    const authCookie = Cookies.get("auth-cookie");
    // console.log(to);
    if (authCookie) {
        if (to.path === "/login") {
            next({ path: "/" });
        } else {
            if (JSON.stringify(store.state.userInfo) === "{}" || !store.state.menus?.length) {
                await initMenus();
                await store.dispatch("getUserInfo");
                next({ ...to, replace: true });
            } else {
                next();
            }
        }
    } else {
        if (to.path === "/login") {
            next();
        } else {
            next({ path: "/login" });
        }
    }
});

export default router;
